//
//  LiveHeaderNode.m
//  EducationLive
//
//  Created by Apple on 2017/9/30.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "LiveHeaderNode.h"
#import "SDCycleScrollView.h"
#import "LiveListModel.h"

@interface LiveHeaderNode ()<SDCycleScrollViewDelegate>

// UI
@property (nonatomic, strong) ASNetworkImageNode *imageNode;
@property (nonatomic, weak) SDCycleScrollView *cycleScrollView;

// Data
@property (nonatomic, strong) NSArray *dataArray;

@end


@implementation LiveHeaderNode

- (instancetype)initWithDataArray:(NSArray *)dataArray
{
    if (self = [super init]) {
        
        _dataArray = dataArray;
        // 加载图片
        [self addImageNode];
    }
    return self;
}
// 加载视图
- (void)didLoad
{
    [super didLoad];
    // 加载图片轮播器
    [self addCycleScrollView];
}
// 加载图片
- (void)addImageNode
{
    ASNetworkImageNode *imageNode = [[ASNetworkImageNode alloc]init];
    imageNode.layerBacked = YES;
    [self addSubnode:imageNode];
    _imageNode = imageNode;
}
// 加载图片轮播器
- (void)addCycleScrollView
{
    // 图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    cycleScrollView.autoScrollTimeInterval = 8;
    cycleScrollView.imageURLStringsGroup = [self liveUrlArray];
    [self.view addSubview:cycleScrollView];
    _cycleScrollView = cycleScrollView;
}
// 创建轮播数组
- (NSMutableArray *)liveUrlArray
{
    NSMutableArray *liveUrlArray = [NSMutableArray array];
    [_dataArray enumerateObjectsUsingBlock:^(CarouselModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [liveUrlArray addObject:obj.img];
    }];
    return liveUrlArray;
}
// layout位置
- (void)layout
{
    [super layout];
    _cycleScrollView.frame = _imageNode.frame;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _imageNode.style.preferredSize = CGSizeMake(kScreenWidth, 180);
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:_imageNode];
    return insetLayout;
}

@end


@interface ContentHeadNode ()

// UI
@property (nonatomic, weak) ASImageNode *imageBgNode;
@property (nonatomic, weak) ASImageNode *imageNode;
@property (nonatomic, weak) ASTextNode *titleNode;
@property (nonatomic, weak) ASTextNode *moreNode;
@property (nonatomic, weak) ASImageNode *moreImageNode;
// Data
@property (nonatomic, strong) GroupModel *groupModel;

@end

@implementation ContentHeadNode

- (instancetype)initWithItem
{
    if (self = [super init]) {
        
//        _groupModel = groupModel;
        // 添加图片背景
        [self addImageBgNode];
        // 添加标题
        [self addTitleNode];
        // 添加更多
        [self addMoreNode];
        // 添加图片
        [self addImageNode];
        // 添加更多图片
        [self addMoreImageNode];
    }
    return self;
}
// 添加图片背景
- (void)addImageBgNode
{
    ASImageNode *imageBgNode = [[ASImageNode alloc]init];
    imageBgNode.backgroundColor = [UIColor clearColor];
    imageBgNode.layerBacked = YES;
    [self addSubnode:imageBgNode];
    _imageBgNode = imageBgNode;
}
// 添加图片
- (void)addImageNode
{
    ASImageNode *imageNode = [[ASImageNode alloc]init];
    imageNode.image = [UIImage imageNamed:@"icon_hot"];
    imageNode.layerBacked = YES;
    [self addSubnode:imageNode];
    _imageNode = imageNode;
}
// 添加图片
- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.layerBacked = YES;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0f] ,NSForegroundColorAttributeName: RGB_255(36, 36, 36)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:@"热门推荐" attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}
// 添加更多
- (void)addMoreNode
{
    ASTextNode *moreNode = [[ASTextNode alloc]init];
    moreNode.layerBacked = YES;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13.0f] ,NSForegroundColorAttributeName: RGB_255(153,153,153)};
    moreNode.attributedText = [[NSAttributedString alloc]initWithString:@"更多" attributes:attrs];
    [self addSubnode:moreNode];
    _moreNode = moreNode;
}
// 添加更多图片
- (void)addMoreImageNode {
    ASImageNode *moreImageNode = [[ASImageNode alloc]init];
    //    moreImageNode.backgroundColor = [UIColor redColor];
    moreImageNode.image = [UIImage imageNamed:@"iocn_more"];
    moreImageNode.layerBacked = YES;
    [self addSubnode:moreImageNode];
    _moreImageNode = moreImageNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _imageBgNode.style.preferredSize = CGSizeMake(kScreenWidth, 29);
    _imageNode.style.preferredSize = CGSizeMake(29, 29);
    _moreImageNode.style.preferredSize = CGSizeMake(10, 8);
    
    ASCenterLayoutSpec *centerSpec = [[ASCenterLayoutSpec alloc] init];
    centerSpec.centeringOptions = ASCenterLayoutSpecCenteringX;
    centerSpec.sizingOptions = ASCenterLayoutSpecSizingOptionMinimumY;
    centerSpec.child = _imageBgNode;
    
    ASStackLayoutSpec *imageStackLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_imageNode,_titleNode]];

    ASStackLayoutSpec *moreStackLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_moreNode,_moreImageNode]];

    ASStackLayoutSpec *headStackLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:5 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[imageStackLayout,moreStackLayout]];
//
    ASOverlayLayoutSpec *headOverlayLayout = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:_imageBgNode overlay:headStackLayout];

    ASInsetLayoutSpec *headLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 5, 0, 10) child:headOverlayLayout];
    
    return headLayout;
    
}

@end
